<?php

class Zhishidi_Express_Adminhtml_Zhishidi_Express_TypeController extends Mage_Adminhtml_Controller_Action {

    public function indexAction() {
        $this->loadLayout();
        $this->getLayout()->getBlock('head')->setTitle(Mage::helper('zhishidi_express')->__('Manage Express Type'));

        $this->renderLayout();
    }

    public function gridAction() {
        $this->loadLayout();
        $this->getResponse()->setBody($this->getLayout()->createBlock('zhishidi_express/adminhtml_type_grid')->toHtml());
    }

    public function editAction() {
        $id = $this->getRequest()->getParam('id');
        $model = Mage::getModel('zhishidi_express/type')->load($id);

        if ($model->getId() || $id == 0) {
            $data = Mage::getSingleton('adminhtml/session')->getExpressTypeData(true);
            if (!empty($data)) {
                $model->setData($data);
            }
            Mage::register('express_type_data', $model);
            $this->loadLayout();
            $this->_setActiveMenu('system/zhishidi_express');
            $this->getLayout()->getBlock('head')->setCanLoadExtJs(true);
            $this->getLayout()->getBlock('head')->setTitle(Mage::helper('zhishidi_express')->__('Manage Express Type'));
            $this->_addContent($this->getLayout()->createBlock('zhishidi_express/adminhtml_type_edit'))->_addLeft($this->getLayout()->createBlock('zhishidi_express/adminhtml_type_edit_tabs'));
            $this->renderLayout();
        } else {
            Mage::getSingleton('adminhtml/session')->addError(Mage::helper('zhishidi_express')->__('Express Type does not exist'));
            $this->_redirect('*/*/');
        }
    }

    public function newAction() {
        $this->_forward('edit');
    }

    public function saveAction() {
        if ($data = $this->getRequest()->getPost()) {
            try {
                $model = Mage::getModel('zhishidi_express/type'); 
 
                $model->setData($data)->setId($this->getRequest()->getParam('id'));

                $model->save();

                Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('zhishidi_express')->__('Type was successfully saved'));
                Mage::getSingleton('adminhtml/session')->setExpressTypeData(false);

                if ($this->getRequest()->getParam('back')) {
                    $this->_redirect('*/*/edit', array('id' => $model->getId()));
                    return;
                }
                $this->_redirect('*/*/');
                return true;
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                Mage::getSingleton('adminhtml/session')->setExpressTypeData($data);
                $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
                return false;
            }
        }
    }

    public function massStatusAction() {
        $typeIds = $this->getRequest()->getParam('mass_action_type');

        if (!is_array($typeIds)) {
            Mage::getSingleton('adminhtml/session')->addError(Mage::helper('zhishidi_express')->__('Please select type'));
        } else {
            try {
                foreach ($typeIds as $type) {
                    $type = Mage::getSingleton('zhishidi_express/type')
                                    ->load($type)
                                    ->setTypeStatus($this->getRequest()->getParam('type_status'))->save();
                }
                Mage::getSingleton('adminhtml/session')->addSuccess(
                        Mage::helper('zhishidi_express')->__(
                                'Total of %d Type(s) were successfully updated', count($typeIds)
                        )
                );
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
            }
        }
        $this->_redirect('*/*/');
    }

    public function massDeleteAction() {
        $typeIds = $this->getRequest()->getParam('mass_action_type');

        if (!is_array($typeIds)) {
            Mage::getSingleton('adminhtml/session')->addError(Mage::helper('zhishidi_express')->__('Please select type'));
        } else {
            try {
                foreach ($typeIds as $type) {
                    $type = Mage::getModel('zhishidi_express/deals')->load($type);
                    $type->delete();
                }
                Mage::getSingleton('adminhtml/session')->addSuccess(
                        Mage::helper('zhishidi_express')->__(
                                'Total of %d Type(s) were successfully deleted', count($typeIds)
                        )
                );
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
            }
        }
        $this->_redirect('*/*/');
    }

    public function deleteAction() {
        if ($this->getRequest()->getParam('id') > 0) {
            try {
                $model = Mage::getModel('zhishidi_express/type');

                $model->setId($this->getRequest()->getParam('id'))
                        ->delete();

                Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Type was successfully deleted'));
                $this->_redirect('*/*/');
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
            }
        }
        $this->_redirect('*/*/');
    }

}

